import requests
#from lxml import etree
from bs4 import BeautifulSoup
url = 'https://news.baidu.com/'
ua = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
req = requests.get(url,headers = ua)
#print("网页返回的内容：",req.content.decode('utf-8'))
html=req.content.decode('utf-8')
#html=etree.HTML(html,parser=etree.HTMLParser(encoding='utf-8'))
#print("title:",html.xpath('/html/head/title'))
soup = BeautifulSoup(html,'lxml')
#print("输出格式化的BeautifulSoup对象：",soup.prettify())
#print("BeautifulSoup对象的类型：",soup)
#print("BeautifulSoup对象的类型：",type (soup))
#print("获取title标签：",soup.title)
#print("获取head标签：",soup.head)
#print("获取第一个a标签：",soup.body.a)
#print("所有a标签：",soup.find_all('a'))
#print("所有a标签的个数：",len(soup.find_all('a')))
tag = soup.a
print("tag的name：",tag.name)
print("tag的内容：",tag)
#tag.name = 'b'
#print("修改name后tag的内容：",tag)
#print("Tag对象的全部属性：",tag.attrs)
#print("href属性的值：",tag['href'])
#tag['id']='wangye'
#print("增加了id属性以后的tag:",tag)
#del tag['data-path']
#print("删除了data-path属性以后的tag:",tag)
#tag['href']='http://wandou.pro/'
#print("修改后tag的内容：",tag)
tag = soup.title
print("Tag对象中包含的字符串：",tag.string)
print("tag.string的类型：",type(tag.string))
print("替换前的tag:",tag)
tag.string.replace_with("豌豆PRO")
print("替换后的tag:",tag)



